import { Data } from '../../utils/data.js';
const app = getApp()
const CONFIG = require('../../config.js')
const WXAPI = require('../../wxapi/main')
Page({
    data: {
        balance: 0.00,
        freeze: 0,
        score: 0,
        score_sign_continuous: 0,
        grids: [0],
        OpenId: '',
        biaoyu: '社区有豪艾，幸福千万家'
    },
    no: function() {
        wx.showToast({
            title: '尚未开通该功能'
        })

    },

    onLoad() {

    },
    onShow() {
        let that = this;
        let OpenId = wx.getStorageSync('OpenId')
        let userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) {
            app.goLoginPageTimeOut()
        } else {
            that.setData({
                userInfo: userInfo,
                version: CONFIG.version,
                OpenId:OpenId,
                vipLevel: app.globalData.vipLevel
            })
        }
        this.getUserApiInfo();
        this.getUserAmount();
    },

    getPhoneNumber: function(e) {
        if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
            wx.showModal({
                title: '提示',
                content: '无法获取手机号码:' + e.detail.errMsg,
                showCancel: false
            })
            return;
        }
        var that = this;
        WXAPI.bindMobile({
            token: wx.getStorageSync('token'),
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
        }).then(function(res) {
            if (res.code === 10002) {
                app.goLoginPageTimeOut()
                return
            }
            if (res.code == 0) {
                wx.showToast({
                    title: '绑定成功',
                    icon: 'success',
                    duration: 2000
                })
                that.getUserApiInfo();
            } else {
                wx.showModal({
                    title: '提示',
                    content: '绑定失败',
                    showCancel: false
                })
            }
        })
    },
    getUserApiInfo: function() {
        var that = this;
        WXAPI.userDetail(wx.getStorageSync('token')).then(function(res) {
            if (res.code == 0) {
                let _data = {}
                _data.apiUserInfoMap = res.data
                if (res.data.base.mobile) {
                    _data.userMobile = res.data.base.mobile
                }
                that.setData(_data);
            }
        })
    },
    getUserAmount: function() {
        var that = this;
        WXAPI.userAmount(wx.getStorageSync('token')).then(function(res) {
            if (res.code == 0) {
                that.setData({
                    balance: res.data.balance.toFixed(2),
                    freeze: res.data.freeze.toFixed(2),
                    score: res.data.score
                });
            }
        })
    },
    relogin: function() {
        app.goLoginPageTimeOut()
    },
    goAsset: function() {
        wx.navigateTo({
            url: "/pages/my/Distributor/Distributor"
        })
  }, 
   gotoQrcode: function() {
    wx.navigateTo({
      url: "/pages/common/qrcode/index"
    })
  }, 
  gowdsc: function () {
    wx.navigateTo({
      url: "/pages/user_goods_collection/index"
    })
  },
    goScore: function() {
        wx.navigateTo({
            url: "/pages/my/Alliance/Alliance"
        })
    },
    goOrder: function(e) {
        console.log(e.currentTarget.dataset.type)
        console.log(e.currentTarget.dataset.type)
        wx.setStorageSync('ordersIndextype', e.currentTarget.dataset.type)
        console.log(wx.getStorageSync('ordersIndextype'))
        wx.switchTab({
            url: "/pages/orders-list/index?type=" + e.currentTarget.dataset.type
        })
    },
    gorouter: function(e) {
        console.log(e.currentTarget.dataset.type)
        wx.navigateTo({
            url: e.currentTarget.dataset.type
        })
    },
    goLogin: function(e) {
        wx.navigateTo({
            url: "/pages/authorize/index"
        });
    }
})